package com.habdqn.easybuy.web.manage.user;

import com.habdqn.easybuy.service.UserService;
import com.habdqn.easybuy.service.impl.UserServiceImpl;
import com.habdqn.easybuy.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Xing.Jiejian
 * @date 2020/11/27 0027
 */
@WebServlet(name = "UserDeleteServlet", urlPatterns = "/manage/user/delete")
public class DeleteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strId = request.getParameter("id");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if (StringUtils.isNotBlank(strId)) {
            int id = Integer.parseInt(strId);
            UserService userService = new UserServiceImpl();
            out.println("<script>");
            if (userService.deleteById(id)) {
                out.println("alert('用户删除成功！');");
                out.println("location.href='" + request.getContextPath() + "/manage/user';");
            } else {
                out.println("alert('用户删除失败！')");
            }
            out.println("</script>");
        }
    }
}
